// 测试

// const { default: axios } = require("axios");


const onfocusFun = e => {
     e=e || window.event;
    var target=e.target || e.srcElement;
    // flag为一说明点击的是题目
    if (target.getAttribute("flag")==1) {
        focusTitleFun(target);
    }
  }
const onblurFun = e => {
     e=e || window.event;
    var target=e.target || e.srcElement;
    if (target.getAttribute("flag")==1) {
        blurTitleFun(target);
    }
  }
//  获取后台数据，渲染页面
var tybody = qu("#tybody");
var page=1,
   pageSize=5,
   datas,
   counts; 
function initFun(){
    var getcounts = ()=> {
        return new Promise((resolve, reject) => {
            axios({
                url:"/admin/faceTitle/findAll",
                method:"GET",
                params: {
                    pageNow:page,
                    pageSize :pageSize
                    }
                }).then(data=>{
                    // console.log(data);
                    // datas=data.data.user.list
                    resolve(data)
                });
        })
        
    }
    getcounts().then(data=>{
        // console.log(data);
        counts=data.data.interviewQuestion.pageTotalCount
        pageSize=data.data.interviewQuestion.pageSize
        datas=data.data.interviewQuestion
        for(let key of datas.list){
            if(key.quessionName=="空"){
                delid = key.id;
                axios({
                    url:"/admin/faceTitle/deleteOneQuextioin",
                    method:"GET",
                    params: {
                        _id:key.id,
                        }
                    }).then(data=>{
                        location.reload();
                    });
                // return getcounts()
            }
        }
        // carryOutFun()
        layui.use(['laypage', 'layer'], function(){
            // console.log(counts);
            let arr_length =new Array();
            if(counts<=5){
                arr_length=[counts];
            }else if(counts<=10&&counts>5){
                arr_length=[5,counts];
            }else if(counts>10&&counts<=20){
                arr_length=[5,10,counts];
            }else if(counts>20){
                arr_length=[5,10,20,counts];
            }
           var laypage = layui.laypage
           ,layer = layui.layer;
           laypage.render({
               elem: 'demo7'
               ,count: counts
               ,curr:page
               ,limit:pageSize
               ,limits: arr_length
               ,layout: ['count', 'prev', 'page', 'next', 'limit']
               ,jump: function(obj){
                //  console.log(obj);
                 page=obj.curr;
                 pageSize=obj.limit;
                 dels=[]
                 checkAll.checked = false;
                 getData(page,pageSize);
                 
                //  changePage();
               }
           });
        });
        
    });
    
}
initFun();
const getData=(a,b)=>{

   // 传参
   if(pageSize>=counts) pageSize=counts;
   // 获得数据
   axios({  
    url:"/admin/faceTitle/findAll",
    method:"GET",
    params: {
        pageNow:a,
        pageSize :b
        }
    })
    .then(function(result){
        datas=result.data.interviewQuestion
        // console.log(datas);
        tybody.innerHTML=''
        let i;
            // for(i=(a-1)*datas.pageSize; i<((a-1)*datas.pageSize+datas.pageSize); i++){
            for(i=0; i<datas.list.length; i++){
                tybody.innerHTML +=`
                <tr id="question_tr">
                    <td><input type="checkbox" class="checkbox" name="${datas.list[i].userId}"  onclick="checkboxFun()" target="${i}"  ></td>
                    <td>${datas.list.length<datas.pageSize?(datas.pageTotalCount-datas.list.length+i+1):datas.start+i+1}</td>
                    <td contentEditable="true" flag="1" class="questions"  onfocus="onfocusFun()" onblur="onblurFun()" name="${datas.list[i].id}" target="${i}">${htmlEscape(datas.list[i].quessionName)}</td>
                    <td>
                        <button class="layui-btn layui-btn-sm layui-btn-sm" onclick="setFocus()" flag="1" target="${i}" style="display:none" >
                            更改
                        </button>
                        <button class="layui-btn layui-btn-danger layui-btn-sm" onclick="delUserFun()" flag="2" target="${datas.list[i].id}">
                        删除
                        </button>
                        
                    </td>
                </tr>
                `
            }
    })
    .catch(err=>{
        // console.log(err)
    })
}

var flagDel = 0;
var delid;
// 删除单个用户
function delUserFun(e){
    e=e || window.event;
   var target=e.target || e.srcElement;
   delid=target.getAttribute("target");
   cover_div.style.display="block"
   windowDiv.style.display="block"
   flagDel = 1;
}
// 批量删除
function delUsersFun(){
    if(dels.length==0){
        // alert("请选中您要删除的面试题目")
        layui.use("layer",function(){
            layer.msg('请选中您要删除的题目！', {
                icon: 7,
                time: 2000  
            })
        })
    }else{
        cover_div.style.display="block"
        windowDiv.style.display="block"
        flagDel = 2;
    }
}
// 查找用户
function seekUsersFun(e){
    let nav_bottom = qu(".nav_bottom")
    if (search_input.value==''){
        // alert("请输入您要查找的用户姓名")
        layui.use("layer",function(){
            layer.msg('请输入您要查找的题目的关键词！', {
                icon: 7,
                time: 2000
            })
        })
    }else{
        axios({
            url:"/admin/faceTitle/seekQusetion",
            method:"GET",
            params: {
                name:search_input.value,
                }
            }).then(data=>{
                console.log(data.data)
                if(data.data.data.writtenQuestion.list.length>0){
                    nav_bottom.style.display="none"
                    tybody.innerHTML=''
                    let i;
                    for(i=0; i<data.data.data.writtenQuestion.list.length; i++){
                        tybody.innerHTML +=`
                        <tr id="question_tr">
                            <td><input type="checkbox" class="checkbox" name="${data.data.data.writtenQuestion.list[i].id}"  onclick="checkboxFun()" target="${i}"  ></td>
                            <td>${i+1}</td>
                            <td contentEditable="true" flag="1" class="questions"  onfocus="onfocusFun()" onblur="onblurFun()" name="${data.data.data.writtenQuestion.list[i].id}" target="${i}">${htmlEscape(data.data.data.writtenQuestion.list[i].quessionName)}</td>
                            <td>
                                <button class="layui-btn layui-btn-sm layui-btn-sm" onclick="setFocus()" flag="1" target="${i}" >
                                    更改
                                </button>
                                <button class="layui-btn layui-btn-danger layui-btn-sm" onclick="delUserFun()" flag="2" target="${data.data.data.writtenQuestion.list[i].id}">
                                删除
                                </button>
                                
                            </td>
                        </tr>
                        `
                    }
                }else{
                  
                    if(search_input.value==0){
                        // location.reload()
                        // alert("您还未输入搜索内容")
                        layui.use("layer",function(){
                            layer.msg('您还未输入搜索内容！', {
                                icon: 7,
                                time: 2000
                            })
                        })
                    }else{
                        // alert("抱歉未找到")
                        layui.use("layer",function(){
                            layer.msg('抱歉未找到！', {
                                icon: 2,
                                time: 2000
                            })
                        })
                    }
                }
                
            });
    }
}
function cancleDelFun(){
    cover_div.style.display="none"
    windowDiv.style.display="none"
    flagDel=0;
}
function submitDelFun(){
    console.log(delid);
    if(flagDel==1){
        axios({
            url:"/admin/faceTitle/deleteOneQuextioin",
            method:"GET",
            params: {
                _id:delid,
                }
            }).then(data=>{
                
                // console.log(data.data)
                if (data.data.err==0) {
                    // getData(page,pageSize);
                    // alert("删除成功")
                    cancleDelFun()
                    layui.use("layer",function(){
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 2000
                        })
                    })
                    // location.reload();

                }
                initFun()
            });
    }else{
        axios({
            url:"/admin/faceTitle/deleteMoreQuextioin",
            method:"GET",
            params: {
                _id:dels,
                }
            }).then(data=>{
                // console.log(data.data);
                initFun()
                if (data.data.err==0) {
                    // getData(page,pageSize);
                    // alert("删除成功")
                    cancleDelFun()
                    layui.use("layer",function(){
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 2000
                        })
                    })
                    // location.reload();
                }
            });
    }
}




// 修改题目
var judgeFlag;
function focusTitleFun(target){
    target.style.backgroundColor = "#68ADCA";
    target.style.color = "#fff";
    judgeFlag=target.innerHTML;
    if(target.innerHTML=="空"||target.innerHTML=="&nbsp;"||target.innerHTML=="&nbsp; &nbsp;"||target.innerHTML=="&nbsp; &nbsp; &nbsp;"||target.innerHTML=="&nbsp; &nbsp; &nbsp; &nbsp;"||target.innerHTML=="&nbsp ;&nbsp; &nbsp; &nbsp; &nbsp;"){
        target.innerHTML=""
    }
}
function blurTitleFun(target){
    target.style.color = "#666";
    target.style.background = "transparent";
    let aaa=target.innerHTML;
        if (aaa ==0||aaa=="&nbsp;"||aaa=="&nbsp; &nbsp;"||aaa=="&nbsp; &nbsp; &nbsp;"||aaa=="&nbsp; &nbsp; &nbsp; &nbsp;"||aaa=="&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;") {
            windowDiv_wrap.innerHTML="监测整行为空，是否删除？"
            delid=target.getAttribute("name");
            console.log(delid);
            cover_div.style.display="block"
            windowDiv.style.display="block"
            flagDel = 1;
            target.innerHTML=judgeFlag
        }else{
            if(target.innerHTML!=judgeFlag){
                changeFun(target.getAttribute("name"),target.innerHTML)
                    // layer.msg('修改成功');
                    layui.use("layer",function(){
                        layer.msg('修改成功', {
                            icon: 1,
                            time: 2000
                        })
                    })
                    // shouSuccessFun()
            }
        }
}
function changeFun(id,question){
    axios({
        url:"/admin/faceTitle/updateQusetion",
        method:"GET",
        params:{
            id:id,
            questionName:question,
            }
        })
        .then(data=>{
        })
        .catch(err=>{
            console.log(err);
        })
}
// 点击修改让该该行问题获取焦点
function setFocus(e){
    e=e || window.event;
    var target=e.target || e.srcElement;
    let index =target.getAttribute("target");
    tybody.getElementsByClassName('questions')[index].focus();
    tybody.getElementsByClassName('questions')[index].backgroundColor="#68ADCA"
    tybody.getElementsByClassName('questions')[index].style.color = "#fff";
}

// 前往添加面试题页面
function goAddFun(){
    location.href="../admin/faceTitle/addFaceTitle"
}

// 提示信息
function shouSuccessFun() {
    let i=0;
    successShow.style.display="block"
    let timer=setInterval(function(){
        if(i<1){
            i++
        }else{
            successShow.style.display="none"
            clearInterval(timer);
        }
    },600)
}